package ibm.cognos.imt

import groovy.util.slurpersupport.GPathResult
import ibm.cognos.imt.dataUsage.DataItemWrapper

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 12/30/13
 * Time: 1:35 PM
 * To change this template use File | Settings | File Templates.
 */
class List extends ReportElement {
    def java.util.List columns = new ArrayList()
    def queryName
    def name
    def rowPerPage = 20
    def owner


    def filter(exp) {
        def filter = new Filter(exp, owner)
//        update query list of the report
//        ListIterator li = owner.queryList.listIterator()
//        while (li.hasNext()) {
//            def query = li.next()
//            if (query.name == queryName) {
//                li.set(query.addFilter(filter))
//            }
//        }
        owner.queryMap[(queryName)].addFilter(filter)
        this
    }

    List(CogReport report, DataItemWrapper... columns) {
        owner = report
        this.columns = columns
//        columns.each {
//            if (it instanceof Fact) {
//                this.fact = it.getDataItem().name
//            }
//        }
    }

    def dataItems() {
        columns
    }

    def rowPerPage(count) {
        rowPerPage = count
        this
    }

    List(GPathResult node) {
//        StringWriter sw = new StringWriter()
//        new XmlNodePrinter(new PrintWriter(sw)).print(node)
//        println("node is: ${sw.toString()}")
        queryName = node.@refQuery
        name = node.@name
    }

    List() {}

    def generateSpec(xmlBuilder) {
        xmlBuilder.list(refQuery: "$queryName", horizontalPagination: "true", name: "$name", rowsPerPage: "$rowPerPage") {
            noDataHandler {
                contents {
                    block {
                        contents {
                            textItem {
                                dataSource {
                                    staticValue("No Data Available")
                                }
                                style {
                                    CSS(value: "padding:10px 18px;")
                                }
                            }
                        }
                    }
                }
            }
            style {
                defaultStyles {
                    defaultStyle(refStyle: "ls")
                }
                CSS(value: "border-collapse:collapse")
            }
            listColumns {
                columns.each {
                    generateColumnSpec(it, xmlBuilder)
                }
            }
        }
    }

    def generateColumnSpec(column, builder) {
        builder.listColumn {
            listColumnTitle {
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "lt")
                    }
                }
                contents {
                    textItem {
                        dataSource {
                            dataItemLabel(refDataItem: "${column.name}")
                        }
                    }
                }
            }
            listColumnBody {
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "lm")
                    }
                }
                contents {
                    textItem {
                        dataSource {
                            dataItemValue(refDataItem: "${column.name}")
                        }
                    }
                }
            }

        }
    }


}


